<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
//引入模型
use App\Http\Models\UserModel;

use App\Http\Requests;
use App\Http\Controllers\Controller;
//引入
use Alert;

//引入加密类
use Illuminate\Support\Facades\Crypt;
use App\Http\Requests\UserAddRequest;
class UserController extends Controller
{

    public function add(UserAddRequest $req)
    {
        if(request()->isMethod('post')){
            $this->validate($req,[
               'username'=>'required|min:2|max:10',
                'password'=>'required|min:2|max:10',
                'email'=>'required|min:5|max:30|email',
                'phoneNumber'=>'required|min:11|max:20',
            ]);
            //准备入库，模型负责处理数据
            $userModel = new UserModel();
            //图片上传
            if($req->hasFile('pic')){//判断文件是否上传
                $file= $req->file('pic');//获取上传的图片对象
                $fileName=uniqid().'.'.$file->getClientOriginalExtension();
                $target="upload";

            }

            //接收数据
            $username = request()->input('username');
            $password = request()->input('password');
            $email = request()->input('email');
            $phoneNumber = request()->input('phoneNumber');

            //模型处理数据
            $userModel = new UserModel();
            $userModel->username=$username;

            $userModel->password=Crypt::encrypt($password);

            $userModel->email=$email;
            $userModel->phoneNumber=$phoneNumber;

            //数据入库
            $rs= $userModel->save();
            if($rs){
                return redirect('/admin/user/lst');
            }else{
                Alert::message('添加失败','提示信息');
                return back();
            }
        }else{
            return view('user.add');
        }


    }


    public function lst()
    {
        //获取数据库中的数据
        $userModel = new UserModel();
        $userData=$userModel::all();

        return view('user.lst',['userData'=> $userData]);
    }

    public function del($id = 0)
    {
        $userModel = new UserModel();
        $useObj = $userModel::find($id);
        $rs = $useObj->delete();
        if($rs){
            return redirect('/admin/user/lst');
        }else{
            return back();
        }

    }

    public function edt($id= 0)
    {
        if(request()->isMethod('post')){
            //接收数据
            $id = request()->input('id');

            $userInfo= UserModel::find($id);
            $userInfo->username = request()->input('username');
            $userInfo->email= request()->input('email');
            $userInfo->phoneNumber= request()->input('phoneNumber');
            if(request()->input('password')){
                $userInfo->password = Crypt::encrypt(request()->input('password'));
            }
            $rs = $userInfo->save();
            if($rs){
                return redirect('admin/user/lst');
            }else{
                return back();
            }
        }else{
            //展示待更新的数据
            //查询当前id对应的数据

             $userInfo = UserModel::find($id);

             return view('user/edt',['userInfo'=>$userInfo]);
        }
    }


    public function update(Request $request, $id)
    {
        //
    }


    public function destroy($id)
    {
        //
    }
}
